package com.example.txttoyy

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.speech.tts.TextToSpeech
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.FileProvider
import androidx.databinding.DataBindingUtil
import com.example.txttoyy.databinding.TxttoyyBinding
import java.io.File
import java.util.*


class MainActivity : AppCompatActivity() {

    private var mTextToSpeech: TextToSpeech? = null

    lateinit var binding: TxttoyyBinding

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)
        requestPermissions(
            arrayOf(
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE
            ), 1
        )
        binding = DataBindingUtil.setContentView(this, R.layout.txttoyy)

        binding.sc.setOnClickListener {

            zhuanyixia(1)
        }
        binding.ld.setOnClickListener {
            zhuanyixia(0)
        }

        binding.fx.setOnClickListener {
            val intent1 = Intent(Intent.ACTION_SEND);
            intent1.type = "audio/*"
            val u = FileProvider.getUriForFile(this, "com.example.txttoyy.fileprovider", f!!)
            intent1.putExtra(Intent.EXTRA_STREAM, u);
            startActivity(Intent.createChooser(intent1, "分享文件"));
        }
    }


    /**
     * 1 转文件
     * 2 朗读
     */
    var f: File? = null
    private fun zhuanyixia(type: Int = 1) {

        mTextToSpeech = TextToSpeech(this) { status ->
            if (status == TextToSpeech.SUCCESS) {
                // 设置语言
                mTextToSpeech?.language = Locale.CHINESE


                mTextToSpeech?.setPitch(0.65f)


                val tv = binding.txt1.text.toString()

//                f = File(
//                    "/storage/emulated/0",
//                    System.currentTimeMillis().toString() + ".wav"
//                )

                f = FileUtils.newFile(System.currentTimeMillis().toString() + ".wav")
//                f?.createNewFile()

                if (type == 1) {
                    val myHashRender = HashMap<String, String>()
                    myHashRender[TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID] = tv
                    val r = mTextToSpeech?.synthesizeToFile(
                        binding.txt1.text.toString(), myHashRender, f!!.absolutePath
                    )

                    if (r == TextToSpeech.SUCCESS) {
                        //保存成功
                        Toast.makeText(this, "成功", Toast.LENGTH_LONG).show()
                    } else {
                        //保存失败
                        Toast.makeText(this, "失败", Toast.LENGTH_LONG).show()
                    }
                } else {
                    mTextToSpeech?.speak(tv, TextToSpeech.QUEUE_FLUSH, null, null)
                }


            }
        }
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out

        String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)

        if (requestCode == 1 && grantResults.all { it == PackageManager.PERMISSION_GRANTED }) {
            // 权限已授予，可以继续操作
        } else {
            // 权限未授予，弹出提示框
//            Toast.makeText(this, "请授予应用程序文件读写权限", Toast.LENGTH_SHORT).show()
        }
    }

}